package com.cskaoyan.service.impl;

import com.cskaoyan.bean.CskaoyanmallFeedback;
import com.cskaoyan.bean.CskaoyanmallFeedbackExample;
import com.cskaoyan.mapper.CskaoyanmallFeedbackMapper;
import com.cskaoyan.service.FeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    CskaoyanmallFeedbackMapper cskaoyanmallFeedbackMapper;

    @Override
    public Map<String, Object> querySelective(Integer userId, String username, Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        CskaoyanmallFeedbackExample feedbackExample = new CskaoyanmallFeedbackExample();
        CskaoyanmallFeedbackExample.Criteria criteria = feedbackExample.createCriteria();

        if (!ObjectUtils.isEmpty(userId)){
            criteria.andUserIdEqualTo(userId);
        }
        if (!ObjectUtils.isEmpty(username)){
            criteria.andUsernameEqualTo(username);
        }
        if (!ObjectUtils.isEmpty(sort)&&!ObjectUtils.isEmpty(order)){
            feedbackExample.setOrderByClause(sort+" "+order);
        }

        List<CskaoyanmallFeedback> feedbacks = cskaoyanmallFeedbackMapper.selectByExample(feedbackExample);
        PageInfo<CskaoyanmallFeedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        long total = feedbackPageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("items",feedbacks);
        map.put("total",total);
        return map;
    }
}
